package com.javaee.ch13.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaee.ch13.common.exceptions.ServiceException;
import com.javaee.ch13.common.pagination.PageResult;
import com.javaee.ch13.entity.Book;
import com.javaee.ch13.entity.BookCategory;
import com.javaee.ch13.entity.BookCopy;
import com.javaee.ch13.mapper.BookMapper;
import com.javaee.ch13.service.BookCategoryService;
import com.javaee.ch13.service.BookCopyService;
import com.javaee.ch13.service.BookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javaee.ch13.web.dto.BookDto;
import com.javaee.ch13.web.dto.queryDto.BookQueryDto;
import com.javaee.ch13.web.vo.BookCategoryVo;
import com.javaee.ch13.web.vo.BookCategoryVo_1;
import com.javaee.ch13.web.vo.BookDetailsVo;
import com.javaee.ch13.web.vo.BookIsbnVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 图书信息表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-04-26
 */
@Service
@Transactional
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {
    @Autowired
    private BookCopyService bookCopyService;

    @Autowired
    BookCategoryService bookCategoryService;

    /**
     * 添加
     */
    @Override
    public void addBook(BookDto addDto) {
        Book book = new Book();
        // 业务逻辑

        // 将dto数据复制到${$objName}实体类}
        BeanUtils.copyProperties(addDto, book);
        // 设置默认字段

        // 保存对象
        if (!this.save(book)) {
            throw new ServiceException("系统错误,图书添加失败!");
        }
    }


    /**
     * 图书分页及多条件查询
     */
    @Override
    public PageResult queryBooks(BookQueryDto queryDto) {
        Page<Book> result = this.lambdaQuery()
                .like(StringUtils.isNotBlank(queryDto.getTitle()), Book::getTitle, queryDto.getTitle())
                .like(StringUtils.isNotBlank(queryDto.getAuthor()), Book::getAuthor, queryDto.getAuthor())
                .eq(StringUtils.isNotBlank(queryDto.getIsbn()), Book::getIsbn, queryDto.getIsbn())
                .like(StringUtils.isNotBlank(queryDto.getPublisher()), Book::getPublisher, queryDto.getPublisher())
                .eq(queryDto.getCategoryId() != null, Book::getCategoryId, queryDto.getCategoryId())
                .eq(queryDto.getStatus() != null, Book::getStatus, queryDto.getStatus())
                .orderByDesc(Book::getCreatedTime)
                .page(queryDto.convertToPage());

        return PageResult.fromPage(result);
    }

    /**
     * 更新
     */
    @Override
    public void updateBook(BookDto updateDto) {
        Book book = new Book();

        // 将dto转换为实体类
        BeanUtils.copyProperties(updateDto, book);

        // 进行增量更新
        if (!this.updateById(book)) {
            throw new ServiceException("系统错误,图书更新失败!");
        }
    }


    @Override
    public List<BookIsbnVO> listIsbnOptions(String q) {
        // isbn 下拉模糊查，不允许超过10位，limit 10
        return this.lambdaQuery().select(Book::getIsbn, Book::getTitle)
                .likeRight(Book::getIsbn, q)
                .last("limit 10")
                .list().stream()
                .map(b -> new BookIsbnVO(b.getIsbn(), b.getTitle()))
                .collect(Collectors.toList());

    }

    @Override
    public Book getBookByIsbn(String isbn) {
        // isbn 查询, 只获取一条记录
        return this.lambdaQuery().eq(Book::getIsbn, isbn).one();
    }

    // 详情,获取图书信息以及所有的图书副本信息
    @Override
    public BookDetailsVo getBookDetailsById(Long id) {
        Book book = this.getById(id);
        BookDetailsVo vo = new BookDetailsVo();
        if (book != null) {
            BeanUtils.copyProperties(book, vo);
            // 查询并填充副本列表
            vo.setBookCopies(bookCopyService.lambdaQuery()
                    .eq(BookCopy::getBookId, id)
                    .list());
        } else {
            throw new ServiceException("图书不存在!");
        }
        return vo;
    }

    @Override
    public BookCategoryVo getBookCategory(Long id) {
        Book book = this.getById(id);
        BookCategoryVo bookCategoryVo = new BookCategoryVo();
        if (book != null) {
            BeanUtils.copyProperties(book, bookCategoryVo);
            bookCategoryVo.setCategory(bookCategoryService.lambdaQuery().eq(BookCategory::getCategoryId, book.getCategoryId()).one());
        } else {
            throw new ServiceException("图书不存在!");
        }
        return bookCategoryVo;
    }

    @Override
    public BookCategoryVo_1 getBookCategory_1(Long id) {
        return baseMapper.selectBooKCategory(id);
    }

}
